package com.baoteng.ui.progress;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Point;
import android.os.Build;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.baoteng.ui.R;

public class CustomProgressDialog extends Dialog {
    private ProgressBar progress;
    private TextView progressMessage;
    private boolean cancelable = true;

    public CustomProgressDialog(Activity context) {
        super(context, R.style.custom_dialog);
        setContentView(R.layout.view_progress_dialog);
        View outerView = findViewById(R.id.rl_content_view);
        outerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (cancelable) {
                    cancel();
                }
            }
        });
        progress = findViewById(R.id.pb_progress);
        progressMessage = findViewById(R.id.tv_message);
        ProgressDrawable drawable = new ProgressDrawable(context, progress);
        progress.setIndeterminateDrawable(drawable);
    }

    public void resetContentViewMargin(int left, int top, int right, int bottom) {
        int screenWidth = getScreenWidth(getContext());
        int screenHeight = getScreenHeight(getContext());

        WindowManager.LayoutParams attr = getWindow().getAttributes();
        attr.gravity = Gravity.NO_GRAVITY;

        attr.width = screenWidth - left - right;
        attr.height = screenHeight - top - bottom;
        attr.x = left;
        attr.y = top;
    }

    public void setMessage(String message) {
        progressMessage.setText(message);
    }

    public void setMessage(int message) {
        setMessage(getContext().getString(message));
    }

    public void setCancelable(boolean cancelable) {
        this.cancelable = cancelable;
    }

    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return context.getResources().getDisplayMetrics().widthPixels;
        }
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.x;
    }

    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (wm == null) {
            return context.getResources().getDisplayMetrics().heightPixels;
        }
        Point point = new Point();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.getDefaultDisplay().getRealSize(point);
        } else {
            wm.getDefaultDisplay().getSize(point);
        }
        return point.y;
    }
}
